package com.jeethink.gs.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @ClassName DateHandleUtils
 * @Description TODO
 * @Author Hyunjin
 * @Date 2022-08-12  00:16
 * @Version 1.0
 **/
public class DateHandleUtils {

    /**
     * 获取连个日期之间相差的月份
     * @param startDate
     * @param endDate
     * @return
     * @throws ParseException
     */
    public static List getMonth(Date startDate, Date endDate) {
        List<String> result = new ArrayList<String>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");

        Calendar tempStart = Calendar.getInstance();
        tempStart.setTime(startDate);
        // 加了一个月
        tempStart.add(Calendar.MONTH, 1);
        Calendar tempEnd = Calendar.getInstance();
        tempEnd.setTime(endDate);
        result.add(sdf.format(startDate));
        while (tempStart.before(tempEnd)) {
            result.add(sdf.format(tempStart.getTime()));
            tempStart.add(Calendar.MONTH, 1);
        }
        return result;
    }

    public static List getQuarter(Date startDate, Date endDate) {
        List<String> result = new ArrayList<String>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");

        Calendar tempStart = Calendar.getInstance();
        tempStart.setTime(startDate);
        // 加了一个月
        tempStart.add(Calendar.MONTH, 3);
        Calendar tempEnd = Calendar.getInstance();
        tempEnd.setTime(endDate);
        result.add(sdf.format(startDate));
        while (tempStart.before(tempEnd)) {
            result.add(sdf.format(tempStart.getTime()));
            tempStart.add(Calendar.MONTH, 3);
        }
        return result;
    }

    public static List getYear(Date startDate, Date endDate) {
        List<String> result = new ArrayList<String>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");

        Calendar tempStart = Calendar.getInstance();
        tempStart.setTime(startDate);
        // 加了一个月
        tempStart.add(Calendar.YEAR, 1);
        Calendar tempEnd = Calendar.getInstance();
        tempEnd.setTime(endDate);
        result.add(sdf.format(startDate));
        while (tempStart.before(tempEnd)) {
            result.add(sdf.format(tempStart.getTime()));
            tempStart.add(Calendar.YEAR, 1);
        }
        return result;
    }
}
